package com.jxy.bean.constant;

import com.jxy.bean.ClassFile;
import com.jxy.tool.Tool;

import java.io.DataInputStream;

public class ModuleInfo extends ConstantPool {
    public int name_index;

    public ModuleInfo(DataInputStream dis, ClassFile cf) {
        super(ConstantPoolTag.CONSTANT_Module.getTag(), cf);
        //长度u2
        name_index = Tool.readU2(dis);
    }

    public String getModuleName() {
        if (valid()) {
            Utf8Info utf8Info = (Utf8Info) cf.getPools()[name_index];
            return utf8Info.getString();
        }
        return "name_index err !";
    }

    @Override
    public String toString() {
        return ConstantPoolTag.CONSTANT_Module.getType() + "{tag:" + tag + ", name_index:" + name_index + "(" + getModuleName() + ")}";
    }

    @Override
    public boolean valid() {
        //name_index必须指向CONSTANT_Utf8_info结构
        if (cf.getPools()[name_index].getTag() == ConstantPoolTag.CONSTANT_Utf8.getTag()) {
            return true;
        }
        return false;
    }
}
